package cn.xinfei.xdecision.data.core.frame.strategy.python;

import cn.xinfei.xdecision.common.model.datax.enums.VariableStrategyType;
import cn.xinfei.xdecision.common.model.datax.exception.XDecisionDataException;
import cn.xinfei.xdecision.common.model.datax.exception.XDecisionDataExceptionType;
import cn.xinfei.xdecision.common.utils.util.OKHttpClientManage;
import cn.xinfei.xdecision.data.model.IVariableRequest;
import cn.xinfei.xdecision.data.model.IVariableResponse;
import cn.xinfei.xdecision.data.core.frame.strategy.BaseStrategy;
import cn.xinfei.xdecision.data.core.frame.strategy.StrategyHandler;
import cn.xinfei.xdecision.model.PythonVarRequest;
import cn.xinfei.xdecision.model.PythonVarResponse;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import java.util.Map;

@StrategyHandler(type = VariableStrategyType.PYTHON)
public class PythonStrategy extends BaseStrategy implements IPythonService {

    private final Logger LOGGER = LoggerFactory.getLogger(PythonStrategy.class);

    @Value("${xdecision.data.python.url}")
    private String pythonVarUrl;
    @Value("${xdecision.data.python.read_timeout}")
    private int pythonReadTimeOut;
    @Value("${xdecision.data.python.connect_timeout}")
    private int pythonConnectTimeOut;

    @Override
    public IVariableResponse process(IVariableRequest request) {
        checkStrategyInput(request);
        PythonVarResponse pythonVarResponse = new PythonVarResponse();

        Map<String, String> rpcContextMap = initRpcContext();
        if (!request.requestStrategys().contains(VariableStrategyType.PYTHON)) {
            LOGGER.info("request ={} 没有包含 [Python] request", JSONObject.toJSONString(request));
        } else {
            OKHttpClientManage okHttpClientManage = OKHttpClientManage.getOkHttpClientManage(pythonVarUrl, pythonReadTimeOut, pythonConnectTimeOut);
            PythonVarRequest pythonVarRequest = (PythonVarRequest) request;
            LOGGER.info("[PythonStrategy] request = {}", JSONObject.toJSONString(pythonVarRequest));
            try {
                String respData = okHttpClientManage.post(JSONObject.toJSONString(pythonVarRequest.getVarCodes(),
                        SerializerFeature.WRITE_MAP_NULL_FEATURES),
                        OKHttpClientManage.MEDIATYPE_JSON, rpcContextMap);
                pythonVarResponse.setData(JSONObject.parseObject(respData));
                pythonVarResponse.setRequestId(pythonVarRequest.getRequestId());
            } catch (Exception e) {
                throw new XDecisionDataException(
                        XDecisionDataExceptionType.DATA_EXECUTOR_ACCESS_PYTHON_ERROR,
                        XDecisionDataExceptionType.DATA_EXECUTOR_ACCESS_PYTHON_ERROR.text + "入参: \n"
                                + JSONObject.toJSONString(request, true),
                        e
                );
            } finally {
                LOGGER.info("requestId ={}  [PythonStrategy] Python 变量输出={}", pythonVarRequest.requestId(), JSONObject.toJSONString(pythonVarResponse.getData(), SerializerFeature.WRITE_MAP_NULL_FEATURES));
            }
        }
        return pythonVarResponse;

    }
}
